﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmScienceManagementInfo : Form
    {

        DialogResult dlgResult;
        EmployeeClient wcfEmployee;
        int _ScienceID;
        bool isChange = false;
        ScienceClient wcfScience;
        ScienceInfo _ScienceInfo;
        RoleRightInfo listRole;
        public frmScienceManagementInfo(int ScienceID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            LoadLanguage();
            _ScienceID = ScienceID;
            listRole = _listRole;
            dlgResult = DialogResult.Cancel;
            wcfScience = Service.Science();
            UISetting.LoadBranch(ref cboBranch);
            _ScienceID = ScienceID;
            _ScienceInfo = new ScienceInfo();
            if (_ScienceID > 0)
            {
                _ScienceInfo = wcfScience.Science_GetInfo(_ScienceID);
                Info2Form();
            }
            isChange = false;
        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_Panel(ref panel2);
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }


        void Info2Form()
        {
            cboBranch.SelectedValue = _ScienceInfo.BranchID;
            cboClassManagement.Text = _ScienceInfo.ClassManagement;
            cboClassOwner.Text = _ScienceInfo.ClassOwner;
            cboDepartment.SelectedValue = _ScienceInfo.DepartmentID;
            cboEmployee.SelectedValue = _ScienceInfo.EmployeeID;
            txtFromDate.Text = _ScienceInfo.FromDate.ToString("dd/MM/yyyy");
            txtToDate.Text = _ScienceInfo.ToDate.ToString("dd/MM/yyyy");
            txtTakePartASName.Text = _ScienceInfo.TakePartAsName;
            txtScienceName.Text = _ScienceInfo.Name;
            txtScienceTenor.Text = _ScienceInfo.Tenor;
            txtResult.Text = _ScienceInfo.Result;
            txtResultDate.Text = _ScienceInfo.ResultDate == DateTime.MinValue ? "" : _ScienceInfo.ResultDate.ToString("dd/MM/yyyy");
            isChange = false;
        }

        void Form2Info()
        {
            DateTime dateVal;
            _ScienceInfo.ID = _ScienceID;
            _ScienceInfo.BranchID = Convert.ToInt32(cboBranch.SelectedValue);
            _ScienceInfo.ClassManagement = cboClassManagement.Text;
            _ScienceInfo.ClassOwner = cboClassOwner.Text;
            _ScienceInfo.DepartmentID = Convert.ToInt32(cboDepartment.SelectedValue);
            _ScienceInfo.EmployeeID = Convert.ToInt32(cboEmployee.SelectedValue);
            _ScienceInfo.FromDate = DateTime.ParseExact(txtFromDate.Text, "dd/MM/yyyy", null);
            _ScienceInfo.ToDate = DateTime.ParseExact(txtToDate.Text, "dd/MM/yyyy", null);
            _ScienceInfo.TakePartAsName = txtTakePartASName.Text;
            _ScienceInfo.Name = txtScienceName.Text;
            _ScienceInfo.Tenor = txtScienceTenor.Text;
            _ScienceInfo.Result = txtResult.Text;
            if (!DateTime.TryParseExact(txtResultDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out dateVal))
                _ScienceInfo.ResultDate = DateTime.MinValue;
            else
                _ScienceInfo.ResultDate = DateTime.ParseExact(txtResultDate.Text, "dd/MM/yyyy", null);
        }


        private void LoadEmployee(int DepartmentID)
        {
            wcfEmployee = Service.Employee();
            cboEmployee.DataSource = null;
            cboEmployee.DisplayMember = "Fullname";
            cboEmployee.ValueMember = "Employee_ID";
            cboEmployee.DataSource = wcfEmployee.Employee_GetWithDepartment(DepartmentID).Tables[0];
            if (cboEmployee.Items.Count == 0)
                cboEmployee.Text = "";
        }

        bool CheckValid()
        {
            DateTime DateVal;
            if (!DateTime.TryParseExact(txtFromDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out DateVal))
            {
                txtFromDate.Focus();
                MessageBox.Show(UpdateUI.GetString("ngaybatdaukhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!DateTime.TryParseExact(txtToDate.Text, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out DateVal))
            {
                txtToDate.Focus();
                MessageBox.Show(UpdateUI.GetString("ngayketthuckhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (_ScienceInfo.ID == 0)
            {
                MessageBox.Show("Bạn muốn lưu trước khi tạo mới");
                return;
            }
            _ScienceID = 0;
            _ScienceInfo = new ScienceInfo();
            txtFromDate.Text = "";
            txtToDate.Text = "";
            txtTakePartASName.Text = "";
            txtScienceName.Text = "";
            txtScienceTenor.Text = "";
            txtResultDate.Text = "";
            txtResult.Text = "";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (CheckValid())
            {
                isChange = true;
                return;
            }
            wcfScience = Service.Science();
            Form2Info();
            if (_ScienceID > 0)//update
            {
                if (!listRole.Update)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (wcfScience.Science_Update(_ScienceInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else//insert
            {
                if (!listRole.Create)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _ScienceInfo.ID = wcfScience.Science_Insert(_ScienceInfo);
                _ScienceID = _ScienceInfo.ID;
                if (_ScienceInfo.ID > 0)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("themmoithanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("themmoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmScienceManagementInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            wcfScience.Close();
            this.DialogResult = dlgResult;
        }

        private void frmScienceManagementInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
            }
        }

        private void cboBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            UISetting.LoadDepartment(ref cboDepartment, Convert.ToInt32(cboBranch.SelectedValue));
        }

        private void cboDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadEmployee(Convert.ToInt32(cboDepartment.SelectedValue));
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }
    }
}
